/*
 * Created by JFormDesigner on Sun Apr 22 13:14:27 CEST 2012
 */

package info.rflow.ui;

import info.rflow.Utils;
import info.rflow.WorkflowContext;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.HashMap;
import java.util.Map;


/**
 * @author User #1
 */
public class WorkingDirectoryPanel extends JPanel {

    Map<File, File> wdHistory = new HashMap<File, File>();
    private WorkflowContext workflowContext;


    public WorkingDirectoryPanel() {
        initComponents();
    }


    public void setWorkflow(WorkflowContext workflowContext) {
        this.workflowContext = workflowContext;

        File workingDirectory = workflowContext.getModel().getWorkingDirectory();
        updateWDField(workingDirectory != null ? workingDirectory.getAbsolutePath() : "");
    }


    private void updateWDField(String wdFileName) {
        wdPathField.setText(wdFileName != null ? wdFileName : "");
    }


    private void changeWDButtonActionPerformed() {
        JFileChooser jfc = new JFileChooser(Utils.getPrefs().get("last.wd.directory", "."));

        jfc.setMultiSelectionEnabled(false);
        jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

        int status = jfc.showOpenDialog(null);
        if (status == JFileChooser.APPROVE_OPTION) {
            File wdLocation = jfc.getSelectedFile();
            String wdFileName = wdLocation.getAbsolutePath();

            updateWDField(wdFileName);
            File scriptWD = new File(wdFileName);
            workflowContext.getModel().setWorkingDirectory(scriptWD);

            Utils.getPrefs().put("last.wd.directory", wdLocation.getParent());
        }
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner non-commercial license
        label2 = new JLabel();
        wdPathField = new JTextField();
        changeWDButton = new JButton();

        //======== this ========
        setLayout(new GridBagLayout());
        ((GridBagLayout) getLayout()).columnWidths = new int[]{0, 0, 0, 0};
        ((GridBagLayout) getLayout()).rowHeights = new int[]{0, 0};
        ((GridBagLayout) getLayout()).columnWeights = new double[]{0.0, 1.0, 0.0, 1.0E-4};
        ((GridBagLayout) getLayout()).rowWeights = new double[]{1.0, 1.0E-4};

        //---- label2 ----
        label2.setText("Working Directory: ");
        add(label2, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 0, 0), 0, 0));

        //---- wdPathField ----
        wdPathField.setEditable(false);
        add(wdPathField, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 0, 0), 0, 0));

        //---- changeWDButton ----
        changeWDButton.setText("setwd");
        changeWDButton.setToolTipText("Change the working directory of the workflow");
        changeWDButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                changeWDButtonActionPerformed();
            }
        });
        add(changeWDButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 0, 0), 0, 0));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }


    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner non-commercial license
    private JLabel label2;
    private JTextField wdPathField;
    private JButton changeWDButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables

}
